package com.shz.controller;


import com.shz.controller.helper.HelperController;
import com.shz.pojo.MenuOrders;
import com.shz.pojo.Orders;
import com.shz.service.MenuOrdersService;
import com.shz.service.OrdersService;
import com.shz.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shz
 * @since 2024-07-29
 */
@Api(tags = "预约培训订单模块")
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @ApiOperation(value = "创建预约订单")
    @PostMapping("/createOrderToCache")
    public void createOrderToCache(@RequestBody Orders s){
        AliPayController.addOrder(s);
    }

    @Resource
    OrdersService ordersService;
    @Resource
    MenuOrdersService menuOrdersService;
    @ApiOperation(value = "我的全部订单由由预约培训表和点菜表组成详细字段注释访问navicat的orders表和menu_orders表")
    @GetMapping("/getMyOrders")
    public List<Object> getMyOrders(){
        Integer id = UserUtil.getCurrentUser().getId();
        List<Object> ans=new ArrayList<>();
        List<Orders> list = ordersService.query().eq("uid", id).list();
        List<MenuOrders> list2 = menuOrdersService.query().eq("uid", id).list();
        ans.addAll(list);
        ans.addAll(list2);
        if(HelperController.isHaveThisOrder()){
            Long globalId = HelperController.getGlobalId();
            Orders haveOrder = AliPayController.getHaveOrder(globalId);
            ans.add(haveOrder);
        }
        for (Object o : ans) {
            if (o instanceof Orders) {
                Orders a = (Orders) o;
                a.setIsShowSignBtn(isShowBtn(a.getBasename(),a.getStartdate()));
            }
        }
        List<Object> sortedAns = ans.stream()
                .sorted(Comparator.comparing(o -> {
                    if (o instanceof Orders) {
                        return ((Orders) o).getGmtpayment();
                    } else if (o instanceof MenuOrders) {
                        return ((MenuOrders) o).getGmtpayment();
                    }
                    return null; // 处理非 Orders 和 MenuOrders 的情况
                }))
                .collect(Collectors.toList());

        return sortedAns;

    }

    @ApiOperation(value = "我的待付款订单")
    @GetMapping("/getMyNotPayOrders")
    public Orders getMyNotPayOrders(){
        if(HelperController.isHaveThisOrder()){
            Long globalId = HelperController.getGlobalId();
            return AliPayController.getHaveOrder(globalId);
        }
        return null;
    }

    @ApiOperation(value = "预约订单签到")
    @PutMapping("/sign")
    public Boolean signOrder(@RequestBody Orders orders){
        orders.setStatus(1);
        return ordersService.updateById(orders);
    }
    @Resource
    StringRedisTemplate stringRedisTemplate;

    public Boolean isShowBtn(String baseName, Date startDate) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化当前日期
        String formattedDate = currentDate.format(formatter);

        // 格式化输入的日期
        LocalDate startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        String startDateFormatted = startLocalDate.format(formatter);

        // 比较日期
        if (startDateFormatted.equals(formattedDate)) {
            return stringRedisTemplate.hasKey("orders:sign:" + baseName + ":" + formattedDate);
        } else {
            return false;
        }
    }


}

